package com.ff.discounts.component.addressdetail

import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.ff.discounts.R
import com.ff.discounts.activity.BaseTitleActivity
import com.ff.discounts.component.address.NewAreaData
import com.ff.discounts.databinding.ActivityAddressDetailBinding
import com.ff.discounts.library.log.HiLog
import com.ff.discounts.util.Constant
import com.lljjcoder.Interface.OnCustomCityPickerItemClickListener
import com.lljjcoder.bean.CustomCityData
import com.lljjcoder.citywheel.CustomConfig
import com.lljjcoder.style.citycustome.CustomCityPicker
import kotlinx.coroutines.launch


/**
 * 收货地址详情
 */
class AddressDetailActivity : BaseTitleActivity<ActivityAddressDetailBinding>() {
    private val resultTv: TextView? = null
    private val visibleItems = 5
    private val isProvinceCyclic = true
    private val isCityCyclic = true

    private val isDistrictCyclic = true

    private val isShowBg = true
    private var customCityPicker: CustomCityPicker? = null

    /**
     * 自定义数据源-省份数据
     */
    private var mProvinceListData: MutableList<CustomCityData> = mutableListOf()
    var mWheelType = CustomConfig.WheelType.PRO_CITY_DIS
    private lateinit var viewModel: AddressDetailViewModel

    /**
     * 返回菜单
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_address_detail, menu)
        return super.onCreateOptionsMenu(menu)
    }

    /**
     * 菜单点击事件
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.save -> {

            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun initDatum() {
        super.initDatum()

        customCityPicker = CustomCityPicker(this)
        // 创建ViewModel
        val addressDetailViewModelFactory =
            AddressDetailViewModelFactory(intent.getStringExtra(Constant.ID))
        viewModel = ViewModelProvider(
            this,
            addressDetailViewModelFactory
        )[AddressDetailViewModel::class.java]
        initViewModel(viewModel)
        viewModel.loadData()

        lifecycleScope.launch {
            viewModel.areaData
                .collect { data ->
                    initCustomeCityData(data)
                }
        }
    }

    override fun initListeners() {
        super.initListeners()
        // 地区点击
        binding.area.setOnClickListener {
            showView()

        }
    }

    private fun initCustomeCityData(data: NewAreaData) {
        var jsPro: CustomCityData?
        var ycCity: CustomCityData?
        var ycDistList: MutableList<CustomCityData> = ArrayList()
        var jsCityList: MutableList<CustomCityData> = ArrayList()
        for ((index, value) in data.provinceData.withIndex()) {
            jsPro = null
            ycCity =null

            jsCityList = ArrayList()
            jsPro = CustomCityData("${value.value}", "${value.label}")
            for ((indexCity, valueCity) in data.cityData[index].withIndex()) {
                ycCity = CustomCityData("${valueCity.value}", "${valueCity.label}")
                jsCityList.add(ycCity)
                ycDistList =  ArrayList()
                for ((indexArea, valueArea) in data.areaData[index][indexCity].withIndex()) {
                    println("index = $indexArea , value = $valueArea,size=${data.areaData[index][indexCity].size}")
                    ycDistList.add(CustomCityData("${valueArea.value}", "${valueArea.label}"))
                    if(indexArea == data.areaData[index][indexCity].size-1){
                        HiLog.d("999999999999999999999")
                        ycCity.list = ycDistList

                        jsPro.list = jsCityList

                    }


                }


            }
            mProvinceListData.add(jsPro)

        }
    }

    private fun showView() {
        val cityConfig = CustomConfig.Builder()
            .title("选择城市")
            .visibleItemsCount(visibleItems)
            .setCityData(mProvinceListData)
            .provinceCyclic(isProvinceCyclic)
            .province("浙江省")
            .city("宁波市")
            .district("鄞州区")
            .cityCyclic(isCityCyclic)
            .setCustomItemLayout(R.layout.item_custome_city) //自定义item的布局
            .setCustomItemTextViewId(R.id.item_custome_city_name_tv)
            .districtCyclic(isDistrictCyclic)
            .drawShadows(isShowBg)
            .setCityWheelType(mWheelType)
            .build()
        customCityPicker?.setCustomConfig(cityConfig)
        customCityPicker?.setOnCustomCityPickerItemClickListener(object :
            OnCustomCityPickerItemClickListener() {
            override fun onSelected(
                province: CustomCityData,
                city: CustomCityData,
                district: CustomCityData
            ) {
                if (province != null && city != null && district != null) {
                    HiLog.d(province.name)
                    resultTv?.text = """province：${province.name}    ${province.id}
                                    city：${city.name}    ${city.id}
                                    area：${district.name}    ${district.id}
                                    """
                } else {
                    resultTv?.text = "结果出错！"
                }
            }
        })
        customCityPicker?.showCityPicker()
    }


}